package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.GoodsTypeService;
import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSGOODSTYPE)
public class BussinessGoodsTypeMController extends BaseMobileController {

    @Autowired
    private GoodsTypeService bussGoodsTypeService;
    @Autowired
    private BusinessService companyService;

    /**
     * 根据商家编号查看商家的商品类别
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findGoodsTypeByBussinessId")
    public void findGoodsTypeByBussinessId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return bussGoodsTypeService.findCompanyGoodsType(bussId);
            }
        });
    }

    /**
     * 添加商品分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addGoodsType")
    public void addGoodsType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String name = getString(json, "name", "");
                Integer orderNumber = getInt(json, "orderNumber", 0);
                return bussGoodsTypeService.addGoodsType(bussId, name, orderNumber);
            }
        });
    }

    /**
     * 修改商品分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateGoodsType")
    public void updateGoodsType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                String name = getString(json, "name", "");
                Integer orderNumber = getInt(json, "orderNumber", 0);
                return bussGoodsTypeService.updateGoodsType(id, name, orderNumber);
            }
        });
    }

    /**
     * 删除商品分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteGoodsType")
    public void deleteGoodsType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                return bussGoodsTypeService.deleteGoodsType(id);
            }
        });
    }

    /**
     * 修改商品类别排序
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateGoodsTypeOrderNum")
    public void updateGoodsTypeOrderNum(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String jsonData = getString(json, "jsonData", "");
                return bussGoodsTypeService.updateGoodsTypeOrderNum(jsonData);
            }
        });
    }

}
